#include <FileSystemObjectBullet.h>
#include <EDesignerCore.h>
#include <iostream>
#include <Functions.h>
#include <btBulletDynamicsCommon.h>
#include <PhysicsWorld.h>
#include <QString>
#include <NodeInfoContainerBullet.h>
#include <BulletClassDataSource/RigidBodyBullet.h>
#include <ObjectInspectorVisitor.h>
class ObjectInspectorVisitorRebuildRigidBody : public ObjectInspectorVisitor
{
public:
    ObjectInspectorVisitorRebuildRigidBody() {}
    virtual ~ObjectInspectorVisitorRebuildRigidBody(){}

    virtual void apply(QStandardItem* item)
    {
        ObjectInspectorItem* objItem = dynamic_cast<ObjectInspectorItem*>(item);
        if(objItem != NULL)
        {
            RigidBodyBullet* rigidBodyBullet = dynamic_cast<RigidBodyBullet*>( objItem->getObjectPtr().data());
            if(rigidBodyBullet != NULL)
            {
                rigidBodyBullet->rebuildRigidBody();
            }
        }
    }
};

FileSystemObjectBullet::FileSystemObjectBullet() : FileSystemObject()
{
}

FileSystemObjectBullet::~FileSystemObjectBullet()
{
}

void FileSystemObjectBullet::beforeLoadFile()
{
    std::cout<<"FileSystemObjectBullet::beforeLoadFile"<<std::endl;
}

void FileSystemObjectBullet::afterLoadFile()
{
    //std::cout<<"FileSystemObjectBullet::afterLoadFile"<<std::endl;
//    const btCollisionObjectArray* collisionObjArray = PhysicsWorld::instance()->getCollisionObjArray();
//    int i;
//    for(i = 0; i < collisionObjArray->size(); i++)
//    {
//        btCollisionObject* object_ptr = collisionObjArray->at(i);

//        NodeInfoContainerBullet* nodeInfo = (NodeInfoContainerBullet*)(object_ptr->getUserPointer());

//        EObject* eobj = nodeInfo->getObjectInspectorItem()->getObjectPtr().data();
//        RigidBodyBullet* rigidBodyBullet = dynamic_cast<RigidBodyBullet*>(eobj);
//        if(rigidBodyBullet != NULL)
//        {
//            rigidBodyBullet->rebuildRigidBody();
//        }

//    }
    NodeInfoContainerBullet* nodeInfo = (NodeInfoContainerBullet*)(PhysicsWorld::instance()->getUserPointer());
    ObjectInspectorVisitorRebuildRigidBody visitor;
    visitor.traverse(nodeInfo->getObjectInspectorItem());
}
